# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================




# ==================================
# Calculating the absolute distances between pairs of candidates in the same district

load("covars_ideal_points_votes.Rdata") # saved as ideal, sep_theta in 5th column

# Identify 1st and 2nd-ranked vote-getters in each district in each election,
# and calculate absolute distances ("dist1").
# Identify Mth and M+1th vote-getters in each district in each election,
# and calculate absolute distances ("dist2").
years <- unique(ideal$year)
all.the.years <- matrix(as.character(NA), ncol=6, nrow=0)
all.the.years <- as.data.frame(all.the.years)
dist1.vector <- c()
dist2.vector <- c()

for(j in 1:length(years)){
  year.dset <- ideal[ideal$year==years[j],]
  kus <- unique(year.dset$ku)
  tops <- matrix(as.character(NA), ncol=6, nrow=0)
  for(i in 1:length(kus)){
    subku <- year.dset[year.dset$ku==kus[i],]
    subkuo <- subku[order(subku$vote, decreasing=T),]
    first <- subkuo[1,"sep_theta"]
    second <- subkuo[2,"sep_theta"]
    dist1 <- abs(first-second)
    mth <- subkuo[unique(subkuo$m), "sep_theta"]
    mplus <- subkuo[(unique(subkuo$m)+1), "sep_theta"]
    dist2 <- abs(mth-mplus)
    dist1.vector <- c(dist1.vector, dist1)
    dist2.vector <- c(dist2.vector, dist2)
    prefecture.ch <- as.character(unique(subkuo$prefecture))
    tops <- rbind(tops, cbind(unique(subkuo$year), unique(subkuo$ku), unique(subkuo$m), dist1, dist2, prefecture.ch))
    }
  all.the.years <- rbind(all.the.years, tops)
}

colnames(all.the.years) <- c("year", "ku", "m", "dist1", "dist2", "prefecture")
sorted.all.the.years <- all.the.years[order(all.the.years[,1], all.the.years[,2]),]

# saved as alt_test_H1.csv"
# We saved this and deleted data for the following districts:
# ku=129 in 1986, 1990 (M of 1)
# ku=1703 in 1996 (JCP cand was miscoded)
# ku=2811 in 1996 (no manifesto for LDP cand)
# ku=2308 in 2005 (no manifesto for NNP cand)
# ku=903 in 2009 (only one candidate)

rm(list=ls())




# ==================================
# Alternative tests of Hypothesis 1 using absolute distances
# Conducting the texts in page 14 of the Appendix (section 3.4)

# Is the variance in absolute distances between the highest-ranked candidate and second-
# ranked candidate in a district larger under the old electoral system than under new?

res <- read.csv("alt_test_H1.csv") 
pre.ER <- res[res$year<1996,]
var(pre.ER$dist1)
post.ER <- res[res$year>1993,]
var(post.ER$dist1)
var.test(pre.ER$dist1, post.ER$dist1, ratio=1)

# Is M associated with greater absolute distances between both sets of
# candidates?

# Create electoral system-specific districts
res$er <- ifelse(res$year>1994, 1, 0)
res$district <- paste(res$ku, res$er, sep="_")

cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

# Four regressions  in Table 2 of Appendix

fit1 <- lm(res$dist1 ~ 
             res$m)
no.controls <- cl(res, fit1, res$district)
print(no.controls)
cat("\n R^2=",summary(fit1)$r.squared,"\n")
cat("\n adj R^2=",summary(fit1)$adj.r.squared,"\n")

fit2 <- lm(res$dist1 ~ 
             res$m + 
             res$prefecture)
controls <- cl(res, fit2, res$district)
print(controls)
cat("\n R^2=",summary(fit2)$r.squared,"\n")
cat("\n adj R^2=",summary(fit2)$adj.r.squared,"\n")

fit3 <- lm(res$dist2 ~ 
             res$m)
no.controls <- cl(res, fit3, res$district)
print(no.controls)
cat("\n R^2=",summary(fit3)$r.squared,"\n")
cat("\n adj R^2=",summary(fit3)$adj.r.squared,"\n")

fit4 <- lm(res$dist2 ~ 
             res$m + 
             res$prefecture)
controls <- cl(res, fit4, res$district)
print(controls)
cat("\n R^2=",summary(fit4)$r.squared,"\n")
cat("\n adj R^2=",summary(fit4)$adj.r.squared,"\n")

rm(list=ls())
